﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Trackr.Core.DataProvider.SQL
{
	public class Delete : IQuery
	{
		private StringBuilder _sql = new StringBuilder(1024);
		private List<string> _filterParams = new List<string>();
		private string _tableName = string.Empty;

		public Delete (string tableName)
		{
			this._tableName = tableName;
		}
		public Delete Where(string key, Operator op, string value)
		{
			_filterParams.Add(string.Format("{0} {1} {2}", key, Enums.GetOperator(op), value));
			return this;
		}
		public Delete And(string key, Operator op, string value)
		{
			_filterParams.Add(string.Format("{0} {1} {2}", key, Enums.GetOperator(op), value));
			return this;
		}

		public string ToSql()
		{
			_sql.Clear();
			_sql.Append(string.Format("DELETE FROM {0}", _tableName));
			
			if (_filterParams.Count > 0)
			{
				_sql.Append(" WHERE ");
				_sql.Append(string.Join(" AND ", _filterParams.ToArray()));
			}
			return _sql.ToString().Trim();
		}
		public override string ToString()
		{
			return ToSql();
		}
	}
}
